


class Node {
    public int val;
    public Node next;//null

    public Node(int val) {
        this.val = val;
    }
}
//链表
public class MyLinkedList {
    public Node head;//标识单链表的头节点

    /**
     * 穷举的方式 创建链表  当然很low。此处只是为了好理解
     */
    public void createList() {
        Node Node1 = new Node(12);
        Node Node2 = new Node(3);
        Node Node3 = new Node(5);
        Node Node4 = new Node(2);
        Node1.next = Node2;
        Node2.next = Node3;
        Node3.next = Node4;
        this.head = Node1;
    }

    /**
     * 打印单链表
     */
    public void show() {
        Node cur = this.head;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到单链表的长度
    public int size() {
        Node cur = this.head;
        int count = 0;
        while (cur != null) {
            count++;//4
            cur = cur.next;
        }
        return count;
    }

    //查找是否包含关键字key是否在单链表当中 15
    public boolean contains(int val) {
        Node cur = this.head;
        while (cur != null) {
            if(cur.val == val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }



    //头插法
    public void addFirst(int data) {
        Node Node = new Node(data);
        if(this.head == null) {
            this.head = Node;
        }else {
            Node.next = this.head;
            this.head = Node;
        }
    }


    //尾插法
    public void addLast(int data) {
        Node Node = new Node(data);
        if(this.head == null) {
            this.head = Node;
        }else {
            Node cur = this.head;
            while (cur.next != null) {
                cur = cur.next;
            }
            cur.next = Node;
        }
    }


    public Node searchPrev(int index) {
        Node cur = this.head;
        int count = 0;
        while(count != index-1) {
            cur = cur.next;
            count++;
        }
        return cur;//
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if(index < 0 || index > size()) {
            System.out.println("下标不合法");
            return;
        }
        //头插法
        if(index == 0) {
            addFirst(data);
            return;
        }
        //尾插法
        if(index == size()) {
            addLast(data);
            return;
        }
        Node cur = searchPrev(index);
        Node Node = new Node(data);
        Node.next = cur.next;
        cur.next = Node;
    }

    public Node searchPrevNode(int val) {
        Node cur = this.head;
        while (cur.next != null) {
            if(cur.next.val == val) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int val) {
        if(this.head == null) return;
        //单独判断头节点的问题
        if(this.head.val == val) {
            this.head = this.head.next;
            return;
        }
        Node cur = searchPrevNode(val);
        if(cur == null) {
            System.out.println("没有你要删除的节点！");
            return;
        }
        Node del = cur.next;
        cur.next = del.next;
    }
    //删除所有值为key的节点
    public void removeAllKey(int val) {
        if(this.head == null) {
            return;
        }
        Node prev = this.head;
        Node cur = this.head.next;
        while (cur != null) {
            if(cur.val == val) {
                prev.next = cur.next;
                cur = cur.next;
            }else {
                prev = cur;
                cur = cur.next;
            }
        }
        //最后判断头节点
        if(this.head.val == val) {
            this.head = this.head.next;
        }
    }

    public void clear(){
        while (this.head != null) {
            Node curNext = this.head.next;
            this.head.next = null;
            this.head = curNext;
        }
    }


    public Node reverseList() {
        if(head == null||head.next == null)
            return head;

        Node cur = head;
        Node newHead = null;

        while(cur != null) {
            Node curNext = cur.next;
            cur.next = newHead;
            newHead = cur;
            cur = curNext;
        }
        return newHead;
    }

    /**
     * 从指定位置开始打印链表
     * @param newHead
     */
    public void show2(Node newHead) {
        Node cur = newHead;
        while(cur != null) {
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }


     //三指针法
    public Node reverseList2() {
        Node cur = this.head;
        Node prev = null;
        Node newHead = null;
        while (cur != null) {
            Node curNext = cur.next;
            if(curNext == null) {
                newHead = cur;
            }
            cur.next = prev;
            prev = cur;
            cur = curNext;
        }
        return newHead;
    }

    public Node middleNode() {
        if(head == null) return head;
        Node fast = head;
        Node slow = head;
        while(fast != null && fast.next!=null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }

    public Node findKthToTail(int k) {
        if(head == null) return null;
        if(k < 0 ) {
            return null;
        }

        Node fast = head;
        Node slow = head;

        while( k-1 != 0) {
            if(fast.next != null) {
                fast = fast.next;
                k--;
            }else{
                return null;
            }
        }

        while(fast.next != null) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }



    //链表分割
    public Node partition( int x) {

        Node cur = this.head;
        Node bs = null;
        Node be = null;
        Node as = null;
        Node ae = null;

        while(cur != null) {
            if(cur.val < x) {
                //第一次插入
                if(bs == null) {
                    bs = cur;
                    be = cur;
                }else {
                    be.next = cur;
                    be = be.next;
                }
            }else {
                //第二部分第一次插入
                if(as == null) {
                    as = cur;
                    ae = cur;
                }else {
                    ae.next = cur;
                    ae = ae.next;
                }
            }
            cur = cur.next;
        }
        /*if(bs == null) {
            return as;
        }else {
            be.next = as;
            ae.next = null;//注意问题，ae可能为空
            return bs;
        }*/
        if(bs == null) {
            return as;
        }
        //bs!=null
        be.next = as;
        if(as != null) {
            ae.next = null;
        }
        return bs;
    }


    //删除链表中重复的节点    尾插法

    public Node deleteDuplication() {

        Node newHead = new Node(-1);
        Node tmp = newHead;
        Node cur = this.head;
        while (cur != null) {
            //cur.next != null : 判断是不是只有一个节点 或者 是不是尾巴节点
            if(cur.next != null && cur.val == cur.next.val) {
                //cur再走的过程当中，有可能剩下的都是相同的
                while ( cur.next != null && cur.val == cur.next.val) {
                    cur = cur.next;
                }
                cur = cur.next;//多走一步
            }else {
                tmp.next = cur;
                tmp = tmp.next;
                cur = cur.next;
            }
        }
        tmp.next = null;//手动设置，防止最后一个节点是重复的
        return newHead.next;
    }


     //链表回文
    public boolean chkPalindrome() {
        if(this.head == null) {
            return true;
        }
        if(this.head.next == null) {
            //只有一个节点
            return true;
        }
        //1、找中间节点
        Node slow = this.head;
        Node fast = this.head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        //slow 指向的节点 就是中间节点
        //2、进行翻转
        Node cur = slow.next;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curNext;
        }
        //翻转完毕 slow指的地方就是 最后一个节点
        while (slow != head) {
            if(slow.val != head.val) {
                return false;
            }
            if(head.next == slow) {
                return true;
            }
            head = head.next;
            slow = slow.next;
        }
        return true;
    }


     //链表是否有环
    public boolean hasCycle1() {
        Node fast = this.head;
        Node slow = this.head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast) {
                return true;
            }
        }
        return false;
    }






    public void createCycle() {
        Node cur = this.head;
        while (cur.next != null) {
            cur = cur.next;
        }
        //cur.next == null
        cur.next = this.head.next;
    }



    public boolean hasCycle() {
        Node fast = this.head;
        Node slow = this.head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast) {
                break;
            }
        }
        if(fast == null || fast.next == null) {
            return false;
        }
        return true;
    }




    //给定一个链表，返回链表开始入环的第一个节点。 如果链表无环，则返回 null
    public Node detectCycle() {
        Node fast = this.head;
        Node slow = this.head;

        while(fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
            if(slow == fast) {
                break;
            }
        }

        if(fast == null || fast.next == null) {
            return null;
        }

        //slow和fast是相遇的
        slow = this.head;
        while (fast != slow) {
            fast = fast.next;
            slow = slow.next;
        }
        return slow;
    }


}








